using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    public class SmallPetMoveComponent : TReusableEvtComponent
    {
        public override void OnCreate(TEntity entity)
        {
            if (entity is not SmallPetEntity smallPetEntity)
            {
                return;
            }

            _smallPetEntity = smallPetEntity;
        }

        public override void OnComponentEvent<T>(TComponentEvent eventType, T arg)
        {
            switch (eventType)
            {
                case TComponentEvent.OnEntityObjectLoaded:
                    if (_smallPetEntity.bindPointType == EntityBindPointSmallPetType.Top)
                    {
                        var flyMoveBehavior = ClassPool<SmallPetFlyMoveBehavior>.Get();
                        moveBehavior = flyMoveBehavior;
                    }
                    else
                    {
                        var followMoveBehavior = ClassPool<SmallPetFollowMoveBehavior>.Get();
                        moveBehavior = followMoveBehavior;
                    }
                    moveBehavior.entity = _smallPetEntity;
                    moveBehavior.Init();
                    break;
                case TComponentEvent.OnEntityObjectHided:
                    DoReset();
                    break;
            }
        }

        public override void OnUpdate(float deltaTime)
        {
            moveBehavior?.OnUpdate(deltaTime);
        }

        public override uint MaxStore => 20;

        public override void OnPoolReset()
        {
            DoReset();
        }

        private void DoReset()
        {
            if (moveBehavior != null)
            {
                ClassPool.PutAutoType(moveBehavior);
                moveBehavior = null;
            }

            _smallPetEntity = null;
        }

        private SmallPetEntity _smallPetEntity;

        private SmallPetAnimationComponent _animComponent;
        public SmallPetBaseMoveBehavior moveBehavior { get; private set; }
    }
}